package com.siemens.industrial.web.mvc.controller;

import com.siemens.industrial.model.admin.SystemConfig;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.WriteLogs;
import com.siemens.industrial.web.mvc.service.admin.SystemConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.CredentialException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by weig on 8/23/16.
 */
@Controller
public class AuthController {
    @Resource
    AuthService authService;

    @Resource
    SystemConfigService systemConfigService;

    @Resource
    WriteLogs writeLogs;

    @RequestMapping(value = "/admin/login", method = RequestMethod.GET)
    public String getAdminLoginPage(String code, ModelMap modelMap) {
        String message = authService.buildMessage(code);
        modelMap.put("message", message);
        return "/admin/login";
    }

    @RequestMapping(value = "/partner/{tenant}/login", method = RequestMethod.GET)
    public String getTenantLoginPage(@PathVariable("tenant") String tenant,String code, ModelMap modelMap) {
        modelMap.put("key", tenant);
        String message = authService.buildMessage(code);
        modelMap.put("message", message);
        return "/partner/login";
    }

    @RequestMapping(value = "/admin/logout", method = RequestMethod.GET)
    public String getAdminLogout() {
        authService.logout();
        return "redirect:admin/login";
    }

    @RequestMapping(value = "/partner/{tenant}/logout", method = RequestMethod.GET)
    public String getTenantLogout(@PathVariable("tenant") String tenant) {
        authService.logout();
        return "redirect:/partner/" + tenant + "/login";
    }

    @RequestMapping(value = "/admin/api/login", method = RequestMethod.POST)
    public String adminLogin(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String, Object> map = authService.attempt(username, password, null);
        //写入登录日志
        if (map.get("isSuccess").equals(true)) {
            com.siemens.industrial.model.admin.User user = (com.siemens.industrial.model.admin.User) authService.currentUser();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String content = user.getUserName() + "于" + simpleDateFormat.format(new Date()) + "登陆了平台。";
            writeLogs.writeLog(SystemConfig.LOG_USER_LOGIN_KEY, content, "platform_login");
            return "redirect:/admin/dashboard?lang=zh#dashboard";
        }
        String code = map.get("code").toString();
        return "redirect:/admin/login?code=" + code;
    }

    @RequestMapping(value = "/partner/{tenant}/api/login", method = RequestMethod.POST)
    public String tenantLogin(@PathVariable("tenant") String tenant, HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map<String, Object> map = authService.attempt(username, password, tenant);
        if (map.get("isSuccess").equals(true)) {
            com.siemens.industrial.model.partner.User user = (com.siemens.industrial.model.partner.User) authService.currentUser();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String content = user.getUserName() + "于" + simpleDateFormat.format(new Date()) + "进行了登录。";
            writeLogs.writeLog(SystemConfig.LOG_USER_LOGIN_KEY, content, tenant + "_login");
            if (map.containsKey("firstLogin")){
                authService.updateFirstLogin(tenant,user.getUserId());
                return "redirect:/partner/"+tenant+"/api/v1/metadata/init?lang=zh#init";
            }
            return "redirect:/partner/"+tenant+"/tenant/device/info?lang=zh#dashboder";
        }
        String code = map.get("code").toString();
        return "redirect:/partner/"+tenant+"/login?code=" + code;
    }
}
